package cn.meetingroom.controller;

import cn.meetingroom.model.Item;
import cn.meetingroom.model.Room;
import cn.meetingroom.service.ItemService;
import org.codehaus.jackson.map.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/ItemServlet/*")
public class ItemServlet extends BaseServlet {
    ItemService itemService = new ItemService();
    ObjectMapper mapper =  new ObjectMapper();

    public void addItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Item item = new Item();
        item.setUserId(Long.parseLong(request.getSession().getAttribute("userId").toString()));
        item.setRoomId(itemService.findRid("room_num",request.getParameter("roomNum")));
        item.setData(request.getParameter("data"));
        item.settInterval(Integer.parseInt(request.getParameter("interval")));
        item.setReason(request.getParameter("reason"));
        item.setState(1);

        if (itemService.addItem(item)) response.sendRedirect("../statics/applySuccess.html");
        else response.sendRedirect("../statics/error.html");
    }

    public void getContent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");

        mapper.writeValue(response.getWriter(),request.getSession().getAttribute("applying"));
    }

    public void applyChange(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        long id = Long.parseLong(request.getParameter("Iid"));
        int state = Integer.parseInt(request.getParameter("state"));

        mapper.writeValue(response.getWriter(),itemService.statechange(id,state));
    }

    public void isExist(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String roomNum = request.getParameter("roomNum");
        String data = request.getParameter("data");
        int interval = Integer.parseInt(request.getParameter("interval"));

        mapper.writeValue(response.getWriter(),itemService.isExist(roomNum,data,interval));
    }

    public void applying(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String,Object> map = new HashMap<>();
        Room room = itemService.getRoom(Integer.parseInt(request.getParameter("rid")));

        String data = request.getParameter("data");
        int interval = Integer.parseInt(request.getParameter("interval"));
        String rNum = room.getNum();
        String rName = room.getRoomName();
        String rPlace = room.getPlace();
        int state = Integer.parseInt(request.getParameter("state"));

        map.put("roomNum",rNum);
        map.put("roomName",rName);
        map.put("roomPlace",rPlace);
        map.put("data",data);
        map.put("interval",interval);
        map.put("state",state);

        request.getSession().setAttribute("userId",Long.parseLong(request.getParameter("uid")));
        request.getSession().setAttribute("applying",map);
    }

    public void userDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String temp = request.getParameter("itemId");
        Long id = Long.parseLong(temp.substring(temp.lastIndexOf('-')+1));

        response.getWriter().print(itemService.userDel(id));
    }
}
